package com.springboot.springbootlogindemo.service.serviceImpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.springbootlogindemo.DTO.SongListDTO;
import com.springboot.springbootlogindemo.domain.Singer;
import com.springboot.springbootlogindemo.domain.Song;
import com.springboot.springbootlogindemo.domain.SongList;
import com.springboot.springbootlogindemo.domain.User;
import com.springboot.springbootlogindemo.mapper.SingerMapper;
import com.springboot.springbootlogindemo.mapper.SongListMapper;
import com.springboot.springbootlogindemo.mapper.SongMapper;
import com.springboot.springbootlogindemo.mapper.UserMapper;


import com.springboot.springbootlogindemo.service.SongService;
import com.springboot.springbootlogindemo.utils.Result;
import com.springboot.springbootlogindemo.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class SongServiceImpl extends ServiceImpl<SongMapper, Song> implements SongService {


    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private SongMapper songMapper;

    @Resource
    private SingerMapper singerMapper;

    /**
     * 添加歌曲
     * @param song
     * @return
     */
    @Override
    public Result addSong(Song song) {
        Song one = query().eq("name", song.getName()).eq("singer_id", song.getSingerId()).one();
        if(one!=null){
            return Result.error("歌曲已存在，无法添加");
        }
        songMapper.insert(song);
        return Result.success(song,"歌曲添加成功");
    }

    /**
     * 更新歌曲
     * @param song
     * @return
     */
    @Override
    public Result updateSong(Song song) {

        return songMapper.updateById(song)>0?Result.success(song,"更新成功"):Result.error("更新失败");
    }

    /**
     * 获取所有歌曲
     * @return
     */
    @Override
    public Result getAllSongs() {
        List<Song> list = query().list();
        return Result.success(list);
    }

    /**
     * 根据歌曲名查找
     * @param name
     * @return
     */
    @Override
    public Result findSongsByName(String name) {
        List<Song> list = query().eq("name", name).list();
        return Result.success(list);
    }

    /**
     * 根据歌手名查找
     * @param singerName
     * @return
     */
    @Override
    public Result findSongsBySinger(String singerName) {
        Singer singer = singerMapper.selectOne(new QueryWrapper<Singer>().eq("name", singerName));
        List<Song> list = query().eq("singer_id", singer.getId()).list();
        return Result.success(list);
    }

    /**
     * 获取指定歌曲(根据歌曲名和歌手名)
     * @param songName
     * @param singerName
     * @return
     */
    @Override
    public Result fingSong(String songName, String singerName) {

        Singer singer = singerMapper.selectOne(new QueryWrapper<Singer>().eq("name", singerName));

        Song song = query().eq("name", songName).eq("singer_id", singer.getId()).one();
        if(song==null){
            Result.error("歌曲不存在");
        }
        return Result.success(song,"查找成功");
    }

}
